import router from "../router/index";
import storage from "./storage";

router.beforeEach((to, from, next) => {
  console.log("[路由跳转]", from.path, "=>", to.path);
  console.log(to, "1", from, "2", next, "3");

  const token = storage.get("token");
  if (token !== null) {
    // 用户有有效的令牌，允许访问页面
    next();
  } else {
    // 用户没有有效的令牌
    if (to.path === "/login") {
      // 用户已经在登录页面，不再重定向
      next();
    } else {
      // 重定向到登录页，并传递原始访问路径作为查询参数
      next({ path: "/login", query: { redirect: to.fullPath } });
    }
  }
});

export default router;
